package ott.adapter;

import java.util.ArrayList;

import oot.activity.FlashScreenActivity;
import oot.activity.ProfileActivity;
import oot.data.Connection;
import oot.data.User;
import oot.tab.activity.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

@SuppressLint({ "ParserError", "ParserError" })
public class FollowerAdapter extends BaseAdapter {
	private ArrayList<User> listUsers;
	private Context context;
	private LayoutInflater mInflater;
	private AQuery aQuery;

	public FollowerAdapter(ArrayList<User> listUsers, Context context) {
		super();
		this.listUsers = listUsers;
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listUsers.size();
	}

	@Override
	public Object getItem(int paramInt) {
		return listUsers.get(paramInt);
	}

	@Override
	public long getItemId(int paramInt) {
		// TODO Auto-generated method stub
		return paramInt;
	}

	@SuppressLint("ParserError")
	@Override
	public View getView(int position, View convertView, ViewGroup paramViewGroup) {
		
		final ViewHolder viewHolder;
		final User user;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listfolloweritem, null);
			viewHolder = new ViewHolder();
			viewHolder.follow = (Button) convertView
					.findViewById(R.id.l_follow);
			viewHolder.username = (TextView) convertView
					.findViewById(R.id.l_username);
			viewHolder.screenName = (TextView) convertView
					.findViewById(R.id.l_screenName);
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		user = (User) getItem(position);
		aQuery = new AQuery(convertView);
		aQuery.id(R.id.l_avatar).image(
				Connection.BASE_URL_IMAGE+user.getUserAvatarURL(), true, true, 0,
				R.drawable.m05_item_avar);

		viewHolder.username.setText(" @"+ user.getUserName() + " " + user.getUserID());
		viewHolder.screenName.setText(user.getUserScreenName());
		
		if (user.isFollowStatus()) {
			viewHolder.follow.setBackgroundResource(R.drawable.bluebutton);
			viewHolder.follow.setText("UnFollow");
		} else {
			viewHolder.follow.setBackgroundResource(R.drawable.greenbutton);
			viewHolder.follow.setText("Follow");
		}

		viewHolder.follow.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View paramView) {
				//Log.e("POS","POS : " + viewHolder.pos);
				if (user.isFollowStatus()) {
					viewHolder.follow.setBackgroundResource(R.drawable.greenbutton);
					viewHolder.follow.setText("Follow");
					unfollow(user.getUserID());
					user.setFollowStatus(false);
				} else {
					viewHolder.follow.setBackgroundResource(R.drawable.bluebutton);
					viewHolder.follow.setText("UnFollow");
					follow(user.getUserID());
					user.setFollowStatus(true);
				}
			}
		});
		if (FlashScreenActivity.user.getUserID().equals(listUsers.get(position).getUserID())) {
			viewHolder.follow.setVisibility(View.GONE);
		} else {
			viewHolder.follow.setVisibility(View.VISIBLE);
		}
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(context,ProfileActivity.class);
				intent.putExtra("user", user);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		});

		return convertView;
	}

	public void follow(String userId) {
		RequestParams requestParams = new RequestParams();
		requestParams.put("followedId", "" + userId);
		Connection.post("follows/follow", requestParams,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String res) {
						super.onSuccess(res);
						Log.e("RES", "" + res);
					}

					@Override
					public void onFailure(Throwable arg0, String res) {
						// TODO Auto-generated method stub
						super.onFailure(arg0, res);
						Log.e("RES", "" + res);
					}
				});
	}

	public void unfollow(String userId) {
		RequestParams requestParams = new RequestParams();
		requestParams.put("followedId", "" + userId);
		Log.e("FID","UFID: " + userId);
		Connection.post("follows/unfollow", requestParams,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String res) {
						super.onSuccess(res);
						Log.e("RES", "" + res);
					}

					@Override
					public void onFailure(Throwable arg0, String res) {
						// TODO Auto-generated method stub
						super.onFailure(arg0, res);
						Log.e("RES", "" + res);
					}
				});
	}

	static class ViewHolder {
		public ImageView avatar;
		public TextView username;
		public TextView screenName;
		public Button follow;
		private int pos;

		public Button getFollow() {
			return follow;
		}

		public void setFollow(Button follow) {
			this.follow = follow;
		}

		public ImageView getAvatar() {
			return avatar;
		}

		public void setAvatar(ImageView avatar) {
			this.avatar = avatar;
		}

		public TextView getUsername() {
			return username;
		}

		public void setUsername(TextView username) {
			this.username = username;
		}

		public TextView getScreenName() {
			return screenName;
		}

		public void setScreenName(TextView screenName) {
			this.screenName = screenName;
		}

		public int getPos() {
			return pos;
		}

		public void setPos(int pos) {
			this.pos = pos;
		}

	}

}
